package sit.mycatalog.ejb.sess;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import sit.mycatalog.ejb.dao.SysuserDao;
import sit.mycatalog.jpa.Sysuser;
import sit.mycatalog.util.AbstractSess;

/**
 * Session Bean implementation class SysuserSess
 */
@Stateless
@LocalBean
public class SysuserSess extends AbstractSess {
	@EJB
	SysuserDao m_daoSysuser;
	
    /**
     * Default constructor. 
     */
    public SysuserSess() {
    }
    
    public Sysuser getSysuser(String p_strUsrid) throws Exception {
    	return m_daoSysuser.findByPrimaryKey(p_strUsrid);
    }
    
    public List<Sysuser> getAll() throws Exception {
    	return m_daoSysuser.findAll();
    }
    
    public boolean createSysuser(Sysuser p_sysuser) throws Exception {
    	return m_daoSysuser.create(p_sysuser);
    }
    
    public boolean updateSysuser(Sysuser p_sysuser) throws Exception {
    	boolean complete = false;
    	
    	Sysuser sysuser = m_daoSysuser.findByPrimaryKey(p_sysuser.getUsrid());
    	
    	if(sysuser.isCheck()) {
    		sysuser.setName(p_sysuser.getName());
    		
    		if((p_sysuser.getPwd() != null) && (!p_sysuser.getPwd().equals(""))) {
    			if((p_sysuser.getNewpwd() != null) && (!p_sysuser.getNewpwd().equals(""))) {
    				if((p_sysuser.getConfirmpwd() != null) && (!p_sysuser.getConfirmpwd().equals(""))) {
    					if(p_sysuser.getNewpwd().equals(p_sysuser.getConfirmpwd())) {
    						sysuser.setPwd(p_sysuser.getNewpwd());
    					}
    				}
    			}
    			
    		}
    		
    		complete = true;
    	}
    	
    	return complete;
    }
    
    public boolean deleteUser(String p_usrid) throws Exception {
    	return m_daoSysuser.remove(p_usrid);
    }
}
